package tasks.LayoutOps;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import resources.tasks.LayoutOps.WrapperHelper;
import tasks.OpportunitiesBaseData.ContentRule;
import tasks.OpportunitiesBaseData.Pattern;

import com.rational.test.ft.*;
import com.rational.test.ft.object.interfaces.*;
import com.rational.test.ft.object.interfaces.SAP.*;
import com.rational.test.ft.object.interfaces.WPF.*;
import com.rational.test.ft.object.interfaces.dojo.*;
import com.rational.test.ft.object.interfaces.siebel.*;
import com.rational.test.ft.object.interfaces.flex.*;
import com.rational.test.ft.object.interfaces.generichtmlsubdomain.*;
import com.rational.test.ft.script.*;
import com.rational.test.ft.value.*;
import com.rational.test.ft.vp.*;
import com.ibm.rational.test.ft.object.interfaces.sapwebportal.*;

import core.Configurations;
import core.ControlsMapping;
import core.controls.Control;
import core.controls.SelectBox;
import core.controls.StaticText;
import core.controls.TextField;
import core.exceptions.ResultFailureException;
import core.layout.Layout;
import dbops.SQLOperator;
/**
 * Description   : Functional Test Script
 * @author angel.tsvetkov
 */
public class Wrapper extends WrapperHelper
{
	private Layout frontEnd = null;
	private Layout database = null;
	private Mapping[] mappings = null;
	
	public Wrapper(Mapping... mappings)
	{
		this.mappings = mappings;
		String[] elementNames = extractVarNames(this.mappings);
		
		frontEnd = new Layout(elementNames);
		frontEnd.setName("Front End");
		
		database = new Layout(elementNames);
		database.setName("Database");
	}
	
	private String[] extractVarNames(Mapping... mappings)
	{
		String[] elementNames = new String[mappings.length];
		
		for(int index = 0; index < mappings.length; index++)
		{
			elementNames[index] = mappings[index].getVarName();
		}
		
		return elementNames;
	}
	
	public void wrapFrontEnd() throws ResultFailureException
	{
		for(Mapping mapping:this.mappings)
		{
			String value = "";
			
			Control control = mapping.getControl();
			
			if(control instanceof StaticText)
			{
				value = ((StaticText)control).getText();
			}
			
			if(control instanceof SelectBox)
			{
				value = ((SelectBox)control).getSelected();
			}
			
			if(control instanceof TextField)
			{
				value = ((TextField)control).getText();
			}
			
			frontEnd.set(mapping.getVarName(), value.trim());
		}
	}
	
	public void wrapDatabase(String rulePath, Pattern... patterns) throws ResultFailureException
	{
		ContentRule rule = new ContentRule(rulePath);
		SQLOperator query = new SQLOperator();
		
		try {
			query.executeSelect(rule.getRule(patterns),database);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	public boolean compare()
	{
		boolean result = false;
		
		if(frontEnd != null && database != null)
		{
			if(frontEnd.isEqual(database))
			{
				result = true;
			}
		}
		
		return result;
	}
}

